package com.punai.customer.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.SecurityUtils;
import com.punai.customer.domain.CusLinkMechanicalStaCus;
import com.punai.customer.domain.CusMechanicalItem;
import com.punai.customer.domain.vo.LinkCusStaVo;
import com.punai.customer.domain.vo.MechanicalStaCusVo;
import com.punai.customer.domain.vo.MechanicallVo;
import com.punai.customer.mapper.CusLinkMechanicalStaCusMapper;
import com.punai.customer.mapper.CusMechanicalChapterMapper;
import com.punai.customer.mapper.CusMechanicalItemMapper;
import com.punai.customer.service.ICusLinkMechanicalStaCusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 标准机械与客户机械的对应Service业务层处理
 *
 * @author zxw_
 * @date 2023-02-02
 */
@Service
public class CusLinkMechanicalStaCusServiceImpl extends ServiceImpl<CusLinkMechanicalStaCusMapper,CusLinkMechanicalStaCus> implements ICusLinkMechanicalStaCusService
{
    @Autowired
    private CusLinkMechanicalStaCusMapper cusLinkMechanicalStaCusMapper;
    @Autowired
    private CusMechanicalItemMapper cusMechanicalItemMapper;
    @Autowired
    private CusMechanicalChapterMapper cusMechanicalChapterMapper;

    /**
     * 查询标准机械与客户机械的对应
     *
     * @param id 标准机械与客户机械的对应主键
     * @return 标准机械与客户机械的对应
     */
    @Override
    public CusLinkMechanicalStaCus selectCusLinkMechanicalStaCusById(String id)
    {
        return cusLinkMechanicalStaCusMapper.selectCusLinkMechanicalStaCusById(id);
    }

    /**
     * 查询标准机械与客户机械的对应列表
     *
     * @param cusLinkMechanicalStaCus 标准机械与客户机械的对应
     * @return 标准机械与客户机械的对应
     */
    @Override
    public List<CusLinkMechanicalStaCus> selectCusLinkMechanicalStaCusList(CusLinkMechanicalStaCus cusLinkMechanicalStaCus)
    {
        return cusLinkMechanicalStaCusMapper.selectCusLinkMechanicalStaCusList(cusLinkMechanicalStaCus);
    }

    /**
     * 新增标准机械与客户机械的对应
     *
     * @param cusLinkMechanicalStaCus 标准机械与客户机械的对应
     * @return 结果
     */
    @Override
    public int insertCusLinkMechanicalStaCus(CusLinkMechanicalStaCus cusLinkMechanicalStaCus)
    {
            return cusLinkMechanicalStaCusMapper.insert(cusLinkMechanicalStaCus);
    }

    /**
     * 修改标准机械与客户机械的对应
     *
     * @param cusLinkMechanicalStaCus 标准机械与客户机械的对应
     * @return 结果
     */
    @Override
    public int updateCusLinkMechanicalStaCus(CusLinkMechanicalStaCus cusLinkMechanicalStaCus)
    {
        return cusLinkMechanicalStaCusMapper.updateById(cusLinkMechanicalStaCus);
    }

    /**
     * 批量删除标准机械与客户机械的对应
     *
     * @param ids 需要删除的标准机械与客户机械的对应主键
     * @return 结果
     */
    @Override
    public int deleteCusLinkMechanicalStaCusByIds(String[] ids)
    {


        return cusLinkMechanicalStaCusMapper.deleteCusLinkMechanicalStaCusByIds(ids);
    }

    /**
     * 删除标准机械与客户机械的对应信息
     *
     * @param id 标准机械与客户机械的对应主键
     * @return 结果
     */
    @Override
    public int deleteCusLinkMechanicalStaCusById(String id)
    {
        Long companyId = SecurityUtils.getCompanyId();
        return cusLinkMechanicalStaCusMapper.deleteBySelfId(companyId,id);
    }

    @Override
    public List<CusLinkMechanicalStaCus> selectUsedLink(LinkCusStaVo vo) {
        Long companyId = SecurityUtils.getCompanyId();
        String staId = vo.getStaId();
        String cusId = vo.getCusId();
        return cusLinkMechanicalStaCusMapper.selectSettingLinkList(companyId,cusId,staId);
    }

    @Override
    public TableDataInfo selectMaterialList(MechanicallVo vo) {
        String linkFlag = vo.getLinkFlag();
        String chapterId = vo.getChapterId();
        String itemName = vo.getItemName();
        Long companyId = SecurityUtils.getCompanyId();
        List<CusMechanicalItem> cusMaterialItems;
        // 需要
        Integer itemCount = -1;

        PageUtils.startPage();
        cusMaterialItems = cusMechanicalItemMapper.selectLinkFlagData(linkFlag,chapterId,companyId,itemName);
        if (StrUtil.isNotEmpty(chapterId)) {
            List<String> chapterIds = cusMechanicalChapterMapper.selectChildrenIdById(chapterId, companyId);
            chapterIds.add(chapterId);
            itemCount = cusMechanicalItemMapper.selectLinkFlagCount(linkFlag, chapterIds, companyId);
        }
        Long total = new PageInfo(cusMaterialItems).getTotal();
        TableDataInfo tableDataInfo = new TableDataInfo(cusMaterialItems, total.intValue());
        tableDataInfo.setMsg(itemCount.toString());
        return tableDataInfo;
    }

    @Override
    public int insertLink(MechanicalStaCusVo vo) {
        Long companyId = SecurityUtils.getCompanyId();
        String cusId = vo.getCusId();
        List<CusLinkMechanicalStaCus> cusLinkMechanicalStaCusList = vo.getCusLinkMechanicalStaCusList();
        for (CusLinkMechanicalStaCus cus : cusLinkMechanicalStaCusList) {
            cus.setCusId(cusId);
            cus.setCompanyId(companyId);
        }
        saveBatch(cusLinkMechanicalStaCusList);
        return 200;
    }
}
